package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BlogContent;
import com.ruoyi.system.domain.BlogLabel;
import com.ruoyi.system.domain.BlogLabelMiddle;
import com.ruoyi.system.mapper.BlogContentMapper;
import com.ruoyi.system.mapper.BlogLabelMiddleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BlogMapper;
import com.ruoyi.system.domain.Blog;
import com.ruoyi.system.service.IBlogService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 博客Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-31
 */
@Service
public class BlogServiceImpl implements IBlogService {

    @Autowired
    private BlogLabelMiddleMapper blogLabelMiddleMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogContentMapper blogContentMapper;

    /**
     * 查询博客
     *
     * @param id 博客主键
     * @return 博客
     */
    @Override
    public Blog selectBlogById(Long id) {
        return blogMapper.selectBlogById(id);
    }

    /**
     * 查询博客列表
     *
     * @param blog 博客
     * @return 博客
     */
    @Override
    public List<Blog> selectBlogList(Blog blog) {
        return blogMapper.selectBlogList(blog);
    }

    /**
     * 新增博客
     *
     * @param blog 博客
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBlog(Blog blog) {
        blog.setCreateTime(DateUtils.getNowDate());
        if (blog.getBlogTypeId() == null) {
            throw new ServiceException("博客类目不能为空");
        }
        if (StringUtils.isEmpty(blog.getBlogContent())) {
            throw new ServiceException("博客内容不能为空");
        }
        int insertBlog = blogMapper.insertBlog(blog);
        Blog searchBlog = new Blog();
        searchBlog.setTitle(blog.getTitle());
        List<Blog> exitBlogs = blogMapper.selectBlogList(searchBlog);
        if (exitBlogs.size()>1){
            throw new ServiceException("当前博客标题已经存在，请修改");
        }
        Blog exitBlog = exitBlogs.get(0);
        if (insertBlog > 0) {
            BlogContent blogContent = new BlogContent();
            blogContent.setContent(blog.getBlogContent());
            blogContent.setCreateTime(DateUtils.getNowDate());
            blogContent.setBlogId(exitBlog.getId());
            blogContentMapper.insertBlogContent(blogContent);
        }
        if (!CollectionUtils.isEmpty(blog.getLabelIds())) {
            for (Long labelId : blog.getLabelIds()) {
                BlogLabelMiddle blogLabelMiddle = new BlogLabelMiddle();
                blogLabelMiddle.setBlogId(exitBlog.getId());
                blogLabelMiddle.setLabelId(labelId);
                blogLabelMiddleMapper.insertBlogLabelMiddle(blogLabelMiddle);
            }
        }
        return insertBlog;
    }

    /**
     * 修改博客
     *
     * @param blog 博客
     * @return 结果
     */
    @Override
    public int updateBlog(Blog blog) {
        if (blog.getId() == null){
            throw new ServiceException("id不能为空");
        }
        blog.setUpdateTime(DateUtils.getNowDate());
        int i = blogMapper.updateBlog(blog);
        if (i > 0){
            BlogContent exitBlogContent = blogContentMapper.selectBlogContentByBlogId(blog.getId());
            exitBlogContent.setContent(blog.getBlogContent());
            blogContentMapper.updateBlogContent(exitBlogContent);
            blogLabelMiddleMapper.deleteByBlogId(blog.getId());
            if (!CollectionUtils.isEmpty(blog.getLabelIds())) {
                for (Long labelId : blog.getLabelIds()) {
                    BlogLabelMiddle blogLabelMiddle = new BlogLabelMiddle();
                    blogLabelMiddle.setBlogId(blog.getId());
                    blogLabelMiddle.setLabelId(labelId);
                    blogLabelMiddleMapper.insertBlogLabelMiddle(blogLabelMiddle);
                }
            }
        }
        return i;
    }

    /**
     * 批量删除博客
     *
     * @param ids 需要删除的博客主键
     * @return 结果
     */
    @Override
    public int deleteBlogByIds(Long[] ids) {
        return blogMapper.deleteBlogByIds(ids);
    }

    /**
     * 删除博客信息
     *
     * @param id 博客主键
     * @return 结果
     */
    @Override
    public int deleteBlogById(Long id) {
        return blogMapper.deleteBlogById(id);
    }
}
